/*
 This file is part of Peers, a java SIP softphone.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
 Copyright 2010 Yohann Martineau 
 */
package net.sourceforge.peers.gui;

import zdh.avatar.softphone.Softphone;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.SwingUtilities;

import net.sourceforge.peers.Logger;
import net.sourceforge.peers.sip.transport.SipRequest;
import net.sourceforge.peers.sip.transport.SipResponse;

public class CallFrame {
    public CallFrameState state;

    public final CallFrameState INIT;
    public final CallFrameState UAC;
    public final CallFrameState UAS;
    public final CallFrameState RINGING;
    public final CallFrameState SUCCESS;
    public final CallFrameState FAILED;
    public final CallFrameState REMOTE_HANGUP;
    public final CallFrameState TERMINATED;

    public CallFrameListener callFrameListener;
    public SipRequest sipRequest;

    public CallFrame(Softphone softphone, String remoteParty,
            String id,
            CallFrameListener callFrameListener,
            Logger logger) {

        INIT = new CallFrameStateInit(id, softphone, this, logger);
        UAC = new CallFrameStateUac(id, softphone, this, logger);
        UAS = new CallFrameStateUas(id, softphone, this, logger);
        RINGING = new CallFrameStateRinging(id, softphone, this, logger);
        SUCCESS = new CallFrameStateSuccess(id, softphone, this, logger);
        FAILED = new CallFrameStateFailed(id, softphone, this, logger);
        REMOTE_HANGUP = new CallFrameStateRemoteHangup(id, softphone, this, logger);
        TERMINATED = new CallFrameStateTerminated(id, softphone, this, logger);

        state = INIT;
        this.callFrameListener = callFrameListener;
    }

    // Methods called by CallFrameState objects.
    public void callClicked() {
        state.callClicked();
    }

    public void incomingCall() {
        state.incomingCall();
    }

    public void remoteHangup() {
        state.remoteHangup();
    }

    public void error(SipResponse sipResponse) {
        state.error(sipResponse);
    }

    public void calleePickup() {
        state.calleePickup();
    }

    public void ringing() {
        state.ringing();
    }

    // Main methods for call transaction
    
    void hangup() {
        if (callFrameListener != null) {
            callFrameListener.hangupClicked(sipRequest);
        }
    }

    void pickup() {
        if (callFrameListener != null && sipRequest != null) {
            callFrameListener.pickupClicked(sipRequest);
        }
    }

    void busyHere() {
        if (callFrameListener != null && sipRequest != null) {
            callFrameListener.busyHereClicked(sipRequest);
            sipRequest = null;
        }
    }

    // Called only by the Softphone in either UAC, UAS, SUCCESS or RINGING state
    
    public void _pickup() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                state.pickupClicked();
            }
        });
    }

    public void _busyHere() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                state.busyHereClicked();
            }
        });
    }

    public void _hangup() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                state.hangupClicked();
            }
        });
    }

    public void setState(CallFrameState state) {
        this.state.log(state);
        this.state = state;
    }

    // delete
    public void addPageEndLabel(String text) {
//        Container container = frame.getContentPane();
//        JLabel label = new JLabel(text);
//        Border labelBorder = BorderFactory.createEmptyBorder(0, 5, 0, 5);
//        label.setBorder(labelBorder);
//        label.setAlignmentX(Component.CENTER_ALIGNMENT);
//        container.add(label);
//        frame.pack();
    }

    public void setSipRequest(SipRequest sipRequest) {
        this.sipRequest = sipRequest;
    }

    public void keypadEvent(char c) {
        callFrameListener.dtmf(c);
    }

}
